package com.example.administrator.daydayappmarket.recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendTopImageFragmentSecondActivity;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendTopImageSpecialSecondActivity;
import com.example.administrator.daydayappmarket.recommend.moduls.AppData;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/12/26.
 */

public class RecommendTopGridViewAdapter extends BaseAdapter {
    private List<AppData> data;
    private Context context;

    public RecommendTopGridViewAdapter(List<AppData> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public AppData getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_main_gridview_top, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final AppData item = getItem(position);
        Picasso.with(context).load(Contacts.URL_BASEIMAGE+item.getImage()).into(viewHolder.topImage);
        viewHolder.topImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(item.getClick_type().equals("tab_topic_info")){
                    String top_topicSecond = Contacts.TAB_TOPIC+item.getId();
                    Intent intent = new Intent(context, RecommendTopImageFragmentSecondActivity.class);
                    intent.putExtra("url",top_topicSecond);
                    context.startActivity(intent);
                }else{
                    String top_topicSecond = Contacts.TTAB_TOPICSPECIAL;
                    Intent intent = new Intent(context, RecommendTopImageSpecialSecondActivity.class);
                    intent.putExtra("url",top_topicSecond);
                    context.startActivity(intent);
                }


            }
        });

        return convertView;
    }

     static class ViewHolder {
        @InjectView(R.id.item_recommend_main_listview_top_image)
        ImageView topImage;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
